package com.skit.common.ktx

import com.skit.common.http.NetResult
import com.skit.common.ktx.NetResultKtx.init

object NetResultKtx {
    fun <T> NetResult<T>.init(
        failed: NetResult.Error<T>.() -> Unit,
        success: NetResult.Success<T>.() -> Unit,
    ) {
        when (this) {
            is NetResult.Error -> {
                failed()
            }
            is NetResult.Success -> {
                success()
            }
        }
    }

    fun <T> NetResult<T>.initData(
        failed: NetResult.Error<T>.() -> Unit,
        success: NetResult.DataSuccess<T>.() -> Unit,
    ) {
        when (this) {
            is NetResult.Error<T> -> {
                failed()
            }
            is NetResult.NoDataSuccess<T> -> {
                error("data为空，请检查服务器参数或请使用init方法")
            }

            is NetResult.DataSuccess<T> -> {
                success()
            }
        }
    }
}